import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wangziyi_app/constans/constans.dart';
import 'package:wangziyi_app/home.dart';
import 'package:wakelock/wakelock.dart';
import 'package:flutter/services.dart';
import 'package:wangziyi_app/scoped_model/app_model.dart';
import 'dart:io';
import 'package:wangziyi_app/scoped_model/app_theme_color_model.dart';
import 'constans/toast.dart';

//创建顶层状态
ThemeColorModel colorModel = ThemeColorModel();
Screnn appMoel=Screnn();
Future<SharedPreferences> sharedPreferences = SharedPreferences.getInstance();

void main() {
  runApp(WangZiYi());
  //修改状态栏颜色
  if (Platform.isAndroid) {
    SystemUiOverlayStyle style =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(style);
  }
  if (Platform.isIOS) {
    SystemUiOverlayStyle style =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(style);
  }
  Wakelock.enable(); //屏幕保持常亮；开发时使用
}

class WangZiYi extends StatefulWidget {
  @override
  _WangZiYiState createState() => _WangZiYiState();
}

class _WangZiYiState extends State<WangZiYi> {
  int appThemeColorIndex; //主题色的索引
  DateTime _lastClickTime; //两次退出程序时间
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 从缓存中读取主题色
    sharedPreferences.then((value) {
      appThemeColorIndex = value.get("themeColor");
      //通知全局使用缓存中的主题色
      colorModel.increment(AppThemeColors().themeColorList[appThemeColorIndex],
          appThemeColorIndex);
    });
  }

  @override
  Widget build(BuildContext context) {

    //TODO 我们在顶层创建了一个CountModel的实例。
    // ScopedModel<T extends Model>是一个StatelessWidget，它接收一个model，并提供给需要它的所有部件。
    // 将ScopedModel<T extends Model>的model属性绑定我们的CountModel对象。
    return ScopedModel<ThemeColorModel>(
        model: colorModel,
        child: ScopedModelDescendant<ThemeColorModel>(
            builder: (context, child, model) {
          return MaterialApp(
              theme: ThemeData(
                  primaryColor: colorModel.appThemeColor,


              ),
              debugShowCheckedModeBanner: false, //去除右上角Debug
              home: WillPopScope(
                  child: Scaffold(body: home()),
                  onWillPop: () async {
                    if (_lastClickTime == null ||
                        DateTime.now().difference(_lastClickTime) >
                            Duration(seconds: 2)) {
                      _lastClickTime = DateTime.now();
                      MyToast().toast("再按一次返回键退出应用");
                      return false;
                    }
                    return true;
                  }),
            builder: (BuildContext context,Widget widget){
              ///设置文字大小不随系统设置改变
              return MediaQuery(child: widget,data:MediaQuery.of(context).copyWith(textScaleFactor: 1.0),);

            },
          );
        }));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
